package com.tengju.support.interfaces;

import com.tengju.bff.interfaces.shared.FeiShuNotifyDataDecrypter;
import com.tengju.bff.interfaces.shared.servlet.NotTokenValid;
import com.tengju.support.application.robot.FeiShuRobotService;
import com.tengju.support.domain.model.robot.FeiShuRobotConversation;
import com.tengju.support.interfaces.vo.FeiShuRobotConversationBody;
import com.tengju.user.domain.shared.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@RestController
@Api(tags = "飞书机器人回调接口")
@RequestMapping(value = "/openapi/feishu/")
public class FeiShuOpenApiController {


    final ExecutorService threadPool = Executors.newSingleThreadExecutor();

    @Autowired
    private FeiShuRobotService feiShuRobotService;

    @ApiOperation(value = "飞书机器人消息会话事件", notes = "飞书机器人消息会话事件")
    @RequestMapping(value = "robot/conversation",method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public String robotConversation(@RequestBody FeiShuRobotConversationBody encryptData) {
        log.info("飞书机器人消息会话事件加密参数: {}", JsonUtil.toJSONString(encryptData));

        try {
            String data = FeiShuNotifyDataDecrypter.decrypt(encryptData.getEncrypt());
            log.info("飞书机器人消息会话事件参数: {}", data);
            FeiShuRobotConversation conversation = JsonUtil.toObject(data, FeiShuRobotConversation.class);

            threadPool.submit(() -> {
                try {
                    feiShuRobotService.feedBack(conversation);
                } catch (Exception e) {
                    log.error("飞书回调处理异常", e);
                }
            });

            return data;
        } catch (Exception e) {
            log.error("receive robot conversation event error, data {}",JsonUtil.toJSONString(encryptData));
            return null;
        }
    }

    @ApiOperation(value = "企业微信飞书机器人消息会话事件", notes = "企业微信飞书机器人消息会话事件")
    @RequestMapping(value = "wx/robot/conversation",method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public String wxRobotConversation(@RequestBody FeiShuRobotConversationBody encryptData) {
        log.info("企业微信飞书机器人消息会话事件加密参数: {}", JsonUtil.toJSONString(encryptData));

        try {
            String data = FeiShuNotifyDataDecrypter.decrypt(encryptData.getEncrypt());
            log.info("企业微信飞书机器人消息会话事件参数: {}", data);
            FeiShuRobotConversation conversation = JsonUtil.toObject(data, FeiShuRobotConversation.class);

            threadPool.submit(() -> {
                try {
                    feiShuRobotService.wxFeedBack(conversation);
                } catch (Exception e) {
                    log.error("飞书回调处理异常", e);
                }
            });

            return data;
        } catch (Exception e) {
            log.error("receive wx robot conversation event error, data {}",JsonUtil.toJSONString(encryptData));
            return null;
        }
    }

}
